/*
 Peyote 3D OpenGL Render
 
 Copyright (c) 2010 Douglas J Lockamy
 
 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:
 
 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.
 
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE.
 */

#include <stdio.h>
 
//#else
#ifdef IOS 

#include <OpenGLES/ES1/gl.h>
#include <OpenGLES/ES1/glext.h>

//#else if DARWIN

#else 
#ifdef DARWIN
#include <OpenGL/gl.h>
#include <OpenGL/glu.h>
#else

#include <GL/gl.h>           
/* this file is needed for X11 applications if we want to use hardware rendering */
#include <GL/glx.h>                                                                
#include <GL/glu.h> 
#endif
#endif


#include <os_window.h>
#include <graphics/GLRender.h>
#include <graphics/RenderObject.h>
#include <graphics/TestObject.h>

GLuint defaultTexture;

extern "C" { 
	int setupGLTexture(char *image, int width, int height, int id);
	int pngLoad(char *file, unsigned long **pwidth, 
				unsigned long **pheight, char **image_data_ptr);
	
};

extern "C" {
	extern int global_isRunning;
	void initGL();
	void renderGL();
	void resizeGL(unsigned int width, unsigned int height);	
#ifdef X11
	extern Display	* display; 
#endif
	extern int	window;
	extern int	doubleBuffered;
#ifndef IOS
	extern GLfloat	rotQuad;
	extern unsigned int width, height;
#endif
}
#ifdef IOS
GLfloat	rotQuad;
unsigned int width, height;
#endif

#ifdef DARWIN
GLfloat	rotQuad;
unsigned int width, height;

void resizeGL(unsigned int width, unsigned int height)
{                                                     
    /* prevent divide-by-zero */                      
    if (height == 0)                                  
        height = 1;                                   
    glViewport(0, 0, width, height);                  
    glMatrixMode(GL_PROJECTION);                      
    glLoadIdentity();                                 
    gluPerspective(45.0f, (GLfloat)width / (GLfloat)height, 0.1f, 100.0f);
    glMatrixMode(GL_MODELVIEW);                                           
} 
#endif

TestObject *cubeObject ;

void GLRender::init(void)
{
	printf("----Render is init\n");
/*	
    glShadeModel(GL_SMOOTH);
    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
#ifndef IOS
    glClearDepth(1.0f); 
#endif
    glEnable(GL_DEPTH_TEST);             
    glDepthFunc(GL_LEQUAL);              
    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);

#ifndef IOS
    resizeGL(width, height);    
#endif
               
    rotQuad = 0;                                                
   // glFlush(); 
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluOrtho2D(-0.1, 1.1, -0.1, 1.1);
	
	glClear(GL_COLOR_BUFFER_BIT);	
	//cubeObject = new TestObject();
	*/
	
	loadTexture(0,0,0);
	
};

//Texture management
int GLRender::loadTexture(char *image, int width, int height)
{
	printf("load textures\n");
	glClearColor(0.0, 0.0, 0.0, 0.0);
	
	glEnable(GL_TEXTURE_2D);
	
	int result;
	long unsigned int *width1, *height1;
	char *image1;
	char *filename = "texture.png";  
	
	glEnable (GL_DEPTH_TEST);
	glPolygonMode (GL_FRONT_AND_BACK, GL_FILL);
	
	/* read the PNG file using pngLoad 
	 * raw data from PNG file is in image buffer
	 */
	result = pngLoad(filename, &width1, &height1, &image1);
	if (result == 0)
    {
		printf("(pngLoad) %s FAILED.\n", filename);
    }
	
	glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
	
	/* Namimg a texture object -see Chapter 9, 
	 * -Red Book
	 */
	glGenTextures(1, &defaultTexture);
	
	int w = 256;//static_cast<long unsigned int*>(width);
	int h = 256;//sprintf("%i",height );
	
	/* take the raw image and put it into an 
	 * OpenGL 2D texture.
	 */
	setupGLTexture(image1, w,h,defaultTexture);//(int *)width, (int *)height, texName);
	//	
	
	
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluOrtho2D(-0.1, 1.1, -0.1, 1.1);
	
	glClear(GL_COLOR_BUFFER_BIT);
	
	glBindTexture(GL_TEXTURE_2D, defaultTexture);
	glColor3f(1.0, 1.0, 1.0);
	glBegin(GL_QUADS); {
		glTexCoord2f(0.0, 0.0); glVertex2f(0.0, 0.0);
		glTexCoord2f(1.0, 0.0); glVertex2f(1.0, 0.0);
		glTexCoord2f(1.0, 1.0); glVertex2f(1.0, 1.0);
		glTexCoord2f(0.0, 1.0); glVertex2f(0.0, 1.0);
	} glEnd();
	
	glFlush();
	
	return -1;
};

int GLRender::loadTextureFromFile(const char* path, int width, int height)
{
	
	return -1;
};

int GLRender::releaseTexture(int texID)
{
	
	return -1;
};	

//Render Managment
int GLRender::BeginFrame(void)
{
	
	return -1;
};

int GLRender::EndFrame(void)
{
	
	return -1;
};

void GLRender::Lock(void)
{

};

void GLRender::UnLock(void)
{

};

void GLRender::ScreenShot(char* buffer, int left,int top, int right, int bottom)
{
	

};

void GLRender::SetViewPort(Camera *camera)
{
	
};

void GLRender::PushTriangles(face &thisFace)
{
	printf("pushing triangles\n");
	
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluOrtho2D(-0.1, 1.1, -0.1, 1.1);
	
	glClear(GL_COLOR_BUFFER_BIT);
	
	glBindTexture(GL_TEXTURE_2D, defaultTexture);
	glColor3f(1.0, 1.0, 1.0);
	glBegin(GL_QUADS); {
		glTexCoord2f(0.0, 0.0); glVertex2f(0.0, 0.0);
		glTexCoord2f(1.0, 0.0); glVertex2f(1.0, 0.0);
		glTexCoord2f(1.0, 1.0); glVertex2f(1.0, 1.0);
		glTexCoord2f(0.0, 1.0); glVertex2f(0.0, 1.0);
	} glEnd();
	
	glFlush();	
	
	
};

//Text Drawing functions
int GLRender::putString(const char* string, point_i location  )
{
	
	return -1;
};

// 2D Drawing functions
int GLRender::drawLine(point_i start, point_i end)
{
	
	return -1;
};

int GLRender::drawCircle(point_i center, int radius)
{
	
	return -1;
};

int GLRender::drawRect(rect_i rect)
{
	
	return -1;
};

int GLRender::drawPoly(void)
{
	
	return -1;
};

int GLRender::fillCircle(point_i center, int radius, int color)
{
	
	return -1;
};

int GLRender::fillRect(rect_i rect, int color)
{
	
	return -1;
};

int GLRender:: fillPolly(void)
{
	
	return -1;
};


